package org.dromara.tearoom.service.impl;

import lombok.RequiredArgsConstructor;

import org.dromara.tearoom.service.ISYWNService;

import org.dromara.tearoom.util.JSONNUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.HashMap;
import java.util.Map;

@RequiredArgsConstructor
@Service
/*云喇叭接口*/
public class SYWNService implements ISYWNService {

    private final RestTemplate restTemplate ;

    @Value("${kuodao.name}")
    private  String name;

    @Value("${kuodao.passwd}")
    private  String passwd;

    @Value(" 86922067 ")
    private  String clientId;
    @Value(" FBC8E8F751EF4BA588CE5D563865AFAA ")
    private  String clientSecret;

    @Value("B527F129C8AE4C55B641DDF7A60299FA")
    private  String deviceId;
    @Override
    public String sYWNLogin() {
        String lgurl = "https://auth.smyoo.com/v1/account/synloginopen";

        // 请求体数据
        Map<String,Object> loginParam =new HashMap<>();
        loginParam.put("appId", 1314);
        loginParam.put("endpointOS",1);
        loginParam.put("areaId",0);
        loginParam.put("clientVersion","");
        loginParam.put("phone",name);
        loginParam.put("password",passwd);
        loginParam.put("deviceId",deviceId);
        loginParam.put("autologin","true");
        loginParam.put("client_id",clientId);
        loginParam.put("client_secret",clientSecret);


        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        // 创建 HttpEntity 对象
        HttpEntity<Map> requestEntity = new HttpEntity<>(loginParam, headers);

        // 调用 POST 接口
        ResponseEntity<Map> response = restTemplate.postForEntity(lgurl, requestEntity, Map.class);
        Map<String,Object> resultMap =JSONNUtil.toEntityWithStatusCheck(response, Map.class);
        if((Integer)resultMap.get("resultCode")!=0)return null;
        Map<String,Object> datamap =(Map) resultMap.get("data");


        String ticketurl = "https://auth.smyoo.com/api/gfriend/synloginticket";

        // 请求体数据
        Map<String,Object> ticketParam =new HashMap<>();
        ticketParam.put("appId", 1314);
        ticketParam.put("endpointOS",1);
        ticketParam.put("ticket",datamap.get("ticket").toString());
        ticketParam.put("deviceId",deviceId);
        ticketParam.put("context","A3C64845E49049BF8EA4027B6828CEF1");
        ticketParam.put("clientVersion","");
        HttpEntity<Map> ticketRequest = new HttpEntity<>(ticketParam, headers);

        // 调用 POST 接口
        ResponseEntity<Map> ticketResponse = restTemplate.postForEntity(ticketurl, ticketRequest, Map.class);
        Map<String,Object> ticketMap =JSONNUtil.toEntityWithStatusCheck(ticketResponse, Map.class);
        if((Integer)ticketMap.get("resultCode")!=0)return null;
        Map<String,Object> ticketDatamap =(Map) ticketMap.get("data");

        return ticketDatamap.get("BpeSessionId").toString();
    }

    @Override
    public Boolean sendSpeakerText(String derviceNo, String cookieValue, Map<String, Object> datapoint) {
        String url = "https://auth.smyoo.com/api/gfriend/speakerplaytext";

        // 请求体数据
        Map<String,Object> param =new HashMap<>();
        param.put("appId", 1314);
        param.put("endpointOS",4);
        param.put("clientVersion","");
        param.put("deviceId",deviceId);
        param.put("mcuid",derviceNo);
        param.put("context","A3C64845E49049BF8EA4027B6828CEF1");
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String jsonStr = objectMapper.writeValueAsString(datapoint);
            param.put("datapoint",jsonStr);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return false;
        }
        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add(HttpHeaders.COOKIE, "BpeSessionId="+cookieValue);
        // 创建 HttpEntity 对象
        HttpEntity<Map> requestEntity = new HttpEntity<>(param, headers);

        // 调用 POST 接口
        ResponseEntity<Map> response = restTemplate.postForEntity(url, requestEntity, Map.class);
        Map<String,Object> resultMap =JSONNUtil.toEntityWithStatusCheck(response, Map.class);
        if((Integer)resultMap.get("resultCode")!=0)return false;

        return true;
    }
}
